package com.api.syh56.shengyunhui.controller.admin;

import com.api.syh56.shengyunhui.common.CommonPage;
import com.api.syh56.shengyunhui.common.CommonResult;
import com.api.syh56.shengyunhui.entity.Slide;
import com.api.syh56.shengyunhui.service.AdminSlideService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 轮播图管理
 */
@RestController
@RequestMapping("admin/slide")
public class AdminSlideController {
    @Autowired
    private AdminSlideService adminSlideService;

    /**
     * 上传图片
     * @param slide
     * @return
     */
    @PostMapping("insert")
    public CommonResult<Slide> uploadSlide(@RequestBody Slide slide){
        adminSlideService.upload(slide);
        return CommonResult.success(slide);
    }

    /**
     * 通过id删除图片
     * @param slide
     * @return
     */
    @PostMapping("delete")
    public CommonResult<Object> deleteById(@RequestBody Slide slide){
        adminSlideService.deleteById(slide.getId());
        return CommonResult.success(null);
    }

    /**
     * 通过id修改图片
     * @param slide
     * @return
     */
    @PostMapping("updata")
    public CommonResult<Slide> updateBy(@RequestBody Slide slide){
        adminSlideService.updateById(slide);
        return CommonResult.success(slide);
    }

    /**
     * 查看所有图片
     * @return
     */
    @GetMapping("selectAll")
    public CommonResult<List<Slide>> selectAll(){
        List<Slide> slides = adminSlideService.selectAll();
        return CommonResult.success(slides);
    }

    /**
     * 分页查询
     * @param pageSize
     * @param pageNum
     * @return
     */
    @GetMapping("selectBy")
    public CommonResult<CommonPage<Slide>> selectBy(@RequestParam String picName,
                                                    @RequestParam(value = "pageSize",defaultValue = "10") Integer pageSize,
                                                    @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum){
        Page<Slide> slides = adminSlideService.selectBy(picName,pageNum,pageSize);
        return CommonResult.success(CommonPage.restPage(slides));
    }
}

